using System;
using System.Drawing;
using System.Windows.Forms;
using ConfigurationSettings;
using ServiceLocator;

namespace CustomControls.UserControls
{
    public partial class UserControlOtherColors : UserControlBase
    {
        ColorDialog _colorDialog1;

        public UserControlOtherColors()
        {
            InitializeComponent();
        }

        public override void Initialize()
        {
            //_configSettings = new ApplicationSettings();
            IApplicationSettings localSettings = Locator.Resolve<IApplicationSettings>();
            _colorDialog1 = new ColorDialog
                {
                    AllowFullOpen = true,
                    AnyColor = true,
                    FullOpen = true,
                    SolidColorOnly = true,
                    ShowHelp = false
                };

            //ListviewBackColor
            buttonListViewBackColor.BackColor = localSettings.ListViewBackColor;
            buttonListViewFocusColor.BackColor = localSettings.FocusColor;
            trackBarFocusOpacity.Value = localSettings.FocusColor.A;

            DirtyFlag = false;
        }

        private void buttonColor_Click(object argSender, EventArgs argEventArgs)
        {
            Button currentButton = (Button)argSender;

            _colorDialog1.CustomColors = new[] {CorrectedColor.CorrectColor(buttonListViewBackColor.BackColor),
                                                    CorrectedColor.CorrectColor(buttonListViewFocusColor.BackColor) };

            _colorDialog1.Color = currentButton.BackColor;

            _colorDialog1.ShowDialog(this);

            Color focusColor = Color.FromArgb(trackBarFocusOpacity.Value, _colorDialog1.Color);
            currentButton.BackColor = focusColor;

            DirtyFlag = true;
        }

        private void trackBarFocusOpacity_ValueChanged(object argSender, EventArgs argEventArgs)
        {
            labelFocusOpacity.Text = trackBarFocusOpacity.Value.ToString();

            Color focusColor = Color.FromArgb(trackBarFocusOpacity.Value, buttonListViewFocusColor.BackColor);
            buttonListViewFocusColor.BackColor = focusColor;

            DirtyFlag = true;
        }

        public override void OnApply() //(object sender, EventArgs argPaintArgs)
        {
            if (!DirtyFlag)
            {
                return;
            }
            IApplicationSettings localSettings = Locator.Resolve<IApplicationSettings>();

            localSettings.ListViewBackColor = buttonListViewBackColor.BackColor;
            localSettings.FocusColor = buttonListViewFocusColor.BackColor;

            localSettings.Save();
            DirtyFlag = false;

        }

        private void button_BackColorChanged(object argSender, EventArgs argEventArgs)
        {
            Control currentControl = (Control)argSender;
            string toolTipString = String.Format("[ R={0}, G={1}, B={2}]", currentControl.BackColor.R, currentControl.BackColor.G, currentControl.BackColor.B);
            toolTip1.SetToolTip(currentControl, toolTipString);

        }

        private void buttonListViewColor_Click(object argSender, EventArgs argEventArgs)
        {
            Button currentButton = (Button)argSender;

            _colorDialog1.CustomColors = new[] {CorrectedColor.CorrectColor(buttonListViewBackColor.BackColor),
                                                    CorrectedColor.CorrectColor(buttonListViewFocusColor.BackColor) };

            _colorDialog1.Color = currentButton.BackColor;

            _colorDialog1.ShowDialog(this);

            currentButton.BackColor = _colorDialog1.Color;

            DirtyFlag = true;

        }
    }
}
